﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 药品信息管理
{
    public partial class ZD_KS : Form
    {
        public ZD_KS()
        {
            InitializeComponent();
        }
        public bool loading_flag = false; int[] eRow = new int[20]; int N = -1;
        private void ZD_KS_Load(object sender, EventArgs e)
        {
            loading_flag = true;
            db db1 = new db();
            string str = "select BM 编码,DH 代号,MC 科室名称 from zd_ks ORDER BY BM";
            dataGridViewX1.DataSource = db1.sqldt(str);
            loading_flag = false;
        }


        private void dataGridViewX1_RowsAdded_1(object sender, DataGridViewRowsAddedEventArgs e)
        {
            MainForm mf = (MainForm)this.MdiParent;
            if (loading_flag == false && mf.loading_flag2 == false)
            {
                N++; 
                eRow[N] = e.RowIndex;
                //MessageBox.Show("触发了一次增加事件");
            }
        }

        private void dataGridViewX1_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            dataGridViewX1.CurrentCell = dataGridViewX1.Rows[0].Cells[0];
            dataGridViewX1.CurrentCell = dataGridViewX1.Rows[eRow[N] - 1].Cells[2];
            db db1 = new db();
            for (int i = 0; i <= N; i++)
            {
                string a="", b="", c="";
                try { a = dataGridViewX1.Rows[eRow[i]-1].Cells[0].Value.ToString(); }
                catch { }
                try { b = dataGridViewX1.Rows[eRow[i]-1].Cells[1].Value.ToString(); }
                catch { }
                try { c = dataGridViewX1.Rows[eRow[i]-1].Cells[2].Value.ToString(); }
                catch { }
                string str = "insert into zd_ks(bm,dh,mc) values ('" + a + "','" + b + "','" + c+"')";
                if (str == "insert into zd_ks(bm,dh,mc) values ('','','')")
                {

                }
                else { db1.sqlcmd(str); }
            }
            N = -1;
            MessageBox.Show("成功添加科室！");

            loading_flag = true;
            string str2 = "select BM 编码,DH 代号,MC 科室名称 from zd_ks ORDER BY BM";
            dataGridViewX1.DataSource = db1.sqldt(str2);
            loading_flag = false;
        }
        

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            db db2 = new db();
            if (MessageBox.Show("确定要删除该类型信息吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                db2.sqlcmd(@"delete from zd_ks where bm='" + dataGridViewX1.CurrentRow.Cells[0].Value.ToString() + "'");
            }
            loading_flag = true;
            string str = "select BM 编码,DH 代号,MC 科室名称 from zd_ks ORDER BY BM";
            dataGridViewX1.DataSource = db2.sqldt(str);
            loading_flag = false;
        }
    }
}



        
